package com.jiantong.work.impl.manager;

import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.user.impl.dao.BreedFactoryDao;
import com.jiantong.user.impl.dao.BreedFactoryStableDao;
import com.jiantong.user.model.BreedFactory;
import com.jiantong.user.model.BreedFactoryStable;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.enums.ProductionReportStatus;
import com.jiantong.work.impl.dao.AnimalMarkDao;
import com.jiantong.work.impl.dao.BreedTransferRecordDao;
import com.jiantong.work.impl.dao.ProductionReportDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.BreedTransferRecord;
import com.jiantong.work.model.ProductionReport;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by mz on 14/8/17.
 * 转群的事务处理
 */
@Component
public class BreedTransferRecordManager {

    private final BreedTransferRecordDao breedTransferRecordDao;

    private final ProductionReportDao productionReportDao;

    private final BreedFactoryStableDao breedFactoryStableDao;

    private final BreedFactoryDao breedFactoryDao;

    private final AnimalMarkDao animalMarkDao;

    private final ReportHandleResumeDao reportHandleResumeDao;


    @Autowired
    public BreedTransferRecordManager(BreedTransferRecordDao breedTransferRecordDao,
                                      ProductionReportDao productionReportDao,
                                      BreedFactoryStableDao breedFactoryStableDao,
                                      AnimalMarkDao animalMarkDao,
                                      ReportHandleResumeDao reportHandleResumeDao,
                                      BreedFactoryDao breedFactoryDao) {
        this.breedTransferRecordDao = breedTransferRecordDao;
        this.productionReportDao = productionReportDao;
        this.breedFactoryStableDao = breedFactoryStableDao;
        this.animalMarkDao = animalMarkDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
        this.breedFactoryDao = breedFactoryDao;
    }

    @Transactional
    public Long createBreedTransferRecord(BreedTransferRecord breedTransferRecord , Integer toStatus){

        Integer fromStatus = breedTransferRecord.getStatus();
        LoginUser loginUser = UserUtil.getCurrentUser();
        String prefix = "ZQ";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,breedTransferRecordDao.findCurrentMaxNumber(prefix));
        breedTransferRecord.setNumber(number);
        if(toStatus != null && !fromStatus.equals(toStatus)){
            breedTransferRecord.setStatus(toStatus);
            breedTransferRecordDao.create(breedTransferRecord);

            ReportHandleResume reportHandleResume = new ReportHandleResume();

            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setReportId(breedTransferRecord.getId());
            reportHandleResume.setReportNumber(breedTransferRecord.getNumber());
            reportHandleResume.setType(DocumentsType.BREED_TRANSFER_RECORD.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);

            reportHandleResumeDao.create(reportHandleResume);

            BreedFactory breedFactory = breedFactoryDao.findById(breedTransferRecord.getBreedFactoryId());

            //从栏舍
            BreedFactoryStable breedFactoryStableFrom = breedFactoryStableDao.findById(breedTransferRecord.getFromStableId());

            //到栏舍
            BreedFactoryStable breedFactoryStableTo = breedFactoryStableDao.findById(breedTransferRecord.getToStableId());

            //从生产记录
            ProductionReport productionReportFrom = buildProductionRecordFrom(breedTransferRecord , breedFactoryStableFrom , breedFactory,loginUser);

            productionReportDao.create(productionReportFrom);

            //到生产记录
            ProductionReport productionReportTo = buildProductionRecordTo(breedTransferRecord , breedFactoryStableTo , breedFactory,loginUser);

            productionReportDao.create(productionReportTo);

            //更新栏舍内的存栏
            breedFactoryStableFrom.setQuantity(0 - breedTransferRecord.getQuantity());

            breedFactoryStableDao.updateFromSC(breedFactoryStableFrom);

            breedFactoryStableTo.setQuantity(breedTransferRecord.getQuantity());

            breedFactoryStableDao.updateFromSC(breedFactoryStableTo);

        }else {
            breedTransferRecordDao.create(breedTransferRecord);
        }

        return breedTransferRecord.getId();
    }

    @Transactional
    public Boolean updateBreedTransferRecord(BreedTransferRecord breedTransferRecord , Integer toStatus){

        Integer fromStatus = breedTransferRecord.getStatus();
        LoginUser loginUser = UserUtil.getCurrentUser();
        Boolean flag = false;
        if(toStatus != null && !fromStatus.equals(toStatus)){
            breedTransferRecord.setStatus(toStatus);
            flag = breedTransferRecordDao.update(breedTransferRecord);

            ReportHandleResume reportHandleResume = new ReportHandleResume();

            reportHandleResume.setUserId(loginUser.getId());
            reportHandleResume.setUserName(loginUser.getName());
            reportHandleResume.setReportId(breedTransferRecord.getId());
            reportHandleResume.setReportNumber(breedTransferRecord.getNumber());
            reportHandleResume.setType(DocumentsType.BREED_TRANSFER_RECORD.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);

            reportHandleResumeDao.create(reportHandleResume);

            BreedFactory breedFactory = breedFactoryDao.findById(breedTransferRecord.getBreedFactoryId());

            //从栏舍
            BreedFactoryStable breedFactoryStableFrom = breedFactoryStableDao.findById(breedTransferRecord.getFromStableId());
            //到栏舍
            BreedFactoryStable breedFactoryStableTo = breedFactoryStableDao.findById(breedTransferRecord.getToStableId());
            //从生产记录
            ProductionReport productionReportFrom = buildProductionRecordFrom(breedTransferRecord , breedFactoryStableFrom , breedFactory,loginUser);

            productionReportDao.create(productionReportFrom);

            //到生产记录
            ProductionReport productionReportTo = buildProductionRecordTo(breedTransferRecord , breedFactoryStableTo , breedFactory,loginUser);

            productionReportDao.create(productionReportTo);

            //更新栏舍内的存栏
            breedFactoryStableFrom.setQuantity(0 - breedTransferRecord.getQuantity());

            breedFactoryStableDao.update(breedFactoryStableFrom);

            breedFactoryStableTo.setQuantity(breedTransferRecord.getQuantity());

            breedFactoryStableDao.update(breedFactoryStableTo);

        }else {
            flag = breedTransferRecordDao.update(breedTransferRecord);
        }

        return flag;
    }

    /**
     * 到的生产记录
     * @param breedTransferRecord 转群对象
     * @param breedFactoryStable 栏舍对象
     * @param breedFactory 养殖场对象
     * @return ProductionReport
     */
    private ProductionReport buildProductionRecordTo(BreedTransferRecord breedTransferRecord,
                                                     BreedFactoryStable breedFactoryStable,
                                                     BreedFactory breedFactory,
                                                     LoginUser loginUser) {
        Integer breedType = breedFactory.getBreedType();
        String prefix = "SC";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,productionReportDao.findCurrentMaxNumber(prefix));

        ProductionReport productionReport = new ProductionReport();

        productionReport.setNumber(number);
        productionReport.setRelateId(breedTransferRecord.getId());
        productionReport.setRelateNumber(breedTransferRecord.getNumber());
        productionReport.setBreedType(breedType);
        Integer breedSecondType = 101;
        Integer unit = 1;
        Integer breedThirdType = breedFactoryStable.getBreedThirdType();
        if (breedType == 1){
            breedSecondType = 101;
            unit = 1;
        }else if (breedType == 2){
            breedSecondType = 201;
            unit = 1;
        }else if (breedType == 3){
            breedSecondType = 301;
            unit = 2;
        }else if (breedType == 4){
            breedSecondType = 0;
            breedThirdType = 0;
            unit = 3;
        }
        productionReport.setBreedSecondType(breedSecondType);
        productionReport.setBreedThirdType(breedThirdType);
        productionReport.setBirthQuantity(0);
        productionReport.setOutQuantity(0);
        productionReport.setDeathQuantity(0);
        productionReport.setInQuantity(breedTransferRecord.getQuantity());
        productionReport.setChangeQuantity(breedTransferRecord.getQuantity());
        productionReport.setUnit(unit);
        productionReport.setStatus(ProductionReportStatus.FINISHED.value());
        return productionReport;
    }

    /**
     * 从的生产记录
     * @param breedTransferRecord 转群对象
     * @param breedFactoryStable 栏舍对象
     * @param breedFactory 养殖场对象
     * @return ProductionReport
     */
    private ProductionReport buildProductionRecordFrom(BreedTransferRecord breedTransferRecord,
                                                       BreedFactoryStable breedFactoryStable,
                                                       BreedFactory breedFactory,
                                                       LoginUser loginUser) {
        Integer breedType = breedFactory.getBreedType();
        String prefix = "SC";
        prefix = Utils.createPrefix(prefix,loginUser);
        String number = Utils.create(prefix,productionReportDao.findCurrentMaxNumber(prefix));

        ProductionReport productionReport = new ProductionReport();

        productionReport.setNumber(number);
        productionReport.setRelateId(breedTransferRecord.getId());
        productionReport.setRelateNumber(breedTransferRecord.getNumber());
        productionReport.setBreedType(breedType);
        Integer breedSecondType = 101;
        Integer unit = 1;
        Integer breedThirdType = breedFactoryStable.getBreedThirdType();
        if (breedType == 1){
            breedSecondType = 101;
            unit = 1;
        }else if (breedType == 2){
            breedSecondType = 201;
            unit = 1;
        }else if (breedType == 3){
            breedSecondType = 301;
            unit = 2;
        }else if (breedType == 4){
            breedSecondType = 0;
            breedThirdType = 0;
            unit = 3;
        }
        productionReport.setBreedSecondType(breedSecondType);
        productionReport.setBreedThirdType(breedThirdType);
        productionReport.setBirthQuantity(0);
        productionReport.setOutQuantity(breedTransferRecord.getQuantity());
        productionReport.setDeathQuantity(0);
        productionReport.setInQuantity(0);
        productionReport.setChangeQuantity((0 - breedTransferRecord.getQuantity()));
        productionReport.setUnit(unit);
        productionReport.setStatus(ProductionReportStatus.FINISHED.value());
        return productionReport;
    }
}
